package com.pragmatic.controller;

import com.alibaba.fastjson.JSON;
import com.pragmatic.cryptography.EncryptMessage;
import com.pragmatic.cryptography.SDKClient;
import com.pragmatic.domain.CommonResponse;
import com.pragmatic.domain.OrderInfo;
import com.pragmatic.domain.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 生产实际操作(泛型对象)
 *
 * @author wangdong
 * @since 2022/9/28 11:19
 */
@Slf4j
@RestController
@RequestMapping("/generic")
public class GenericController {

  @Resource private SDKClient sdkClient;

  /**
   * 查询用户信息
   *
   * @param userMessageStr 用户对象信息
   */
  @RequestMapping(value = "/getUserInfo", method = RequestMethod.POST)
  public String getUserInfoCryptography(@RequestBody String userMessageStr) {
    log.info("密文-接受的信息={}", userMessageStr);
    // 验签、解密
    String decrypt = sdkClient.doDecrypt(userMessageStr);
    log.info("密文-验签/解密后-明文数据={}", decrypt);
    // 加密、加签
    CommonResponse<UserInfo> response = new CommonResponse<>();
    response.setCode("000000");
    response.setMessage("交易成功");
    UserInfo userInfo = new UserInfo();
    userInfo.setUserName("resp张三");
    userInfo.setPhone("resp162334458221");
    userInfo.setSex("resp男");
    response.setData(userInfo);
    EncryptMessage encryptMessage = sdkClient.doEncrypt(JSON.toJSONString(response));
    // 返回结果
    return JSON.toJSONString(encryptMessage);
  }

  /**
   * 查询订单
   *
   * @param orderMessageStr 订单对象信息
   */
  @RequestMapping(value = "/getOrderInfo", method = RequestMethod.POST)
  public String getOrderInfoCryptography(@RequestBody String orderMessageStr) {
    log.info("密文-接受的信息={}", orderMessageStr);
    // 验签、解密
    String decrypt = sdkClient.doDecrypt(orderMessageStr);
    log.info("密文-验签/解密后-明文数据={}", decrypt);
    // 加密、加签
    CommonResponse<OrderInfo> response = new CommonResponse<>();
    response.setCode("000000");
    response.setMessage("交易成功");
    OrderInfo orderInfo = new OrderInfo();
    orderInfo.setOrderId("res1000012131");
    orderInfo.setOrderName("res水杯");
    orderInfo.setNum(20);
    response.setData(orderInfo);
    EncryptMessage encryptMessage = sdkClient.doEncrypt(JSON.toJSONString(response));
    // 返回结果
    return JSON.toJSONString(encryptMessage);
  }
}
